/*
 * Copyright (C) 2016 JetRadar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jetradarmobile.snowfall;

import java.security.SecureRandom;
import java.util.Random;

public class Randomizer {
    private SecureRandom random = new SecureRandom();

    /**
     * 随机double
     *
     * @param max
     * @return double值
     */
    public Double randomDouble(int max) {
        return random.nextDouble() * (max + 1);
    }

    /**
     * 重载方法获取随机int值
     *
     * @param min
     * @param max
     * @param gaussian
     * @return int值
     */
    public int randomInt(int min, int max, boolean gaussian) {
        return randomInt(max - min, gaussian) + min;
    }

    /**
     * 随机int值
     *
     * @param max
     * @param gaussian
     * @return int值
     */
    public int randomInt(int max, boolean gaussian) {
        if (gaussian) {
            return (int) (Math.abs(randomGaussian()) * (max + 1));
        } else {
            return random.nextInt(max + 1);
        }
    }

    /**
     * 获取随机double值
     *
     * @return double
     */
    public double randomGaussian() {
        double gaussian = random.nextGaussian() / 3;
        if (gaussian > -1 && gaussian < 1) {
            return gaussian;
        } else {
            return randomGaussian();
        }
    }

    /**
     * 通过随机boolean值来获取1/-1
     *
     * @return 1/-1
     */
    public int randomSignum() {
        if (random.nextBoolean()) {
            return 1;
        } else {
            return -1;
        }
    }
}
